package me.obstsalat.guildera;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.HashMap;

public class configParser {
	private guildera plugin;
	private HashMap<String, String> cf = new HashMap<String, String>();
	
	configParser(guildera pl) {
		plugin = pl;
	}
	
	public void load() {
		try {
			String path = "";
			File file = null;
			path = plugin.getDataFolder().getCanonicalPath();
			file = new File(path);
			if(!file.exists()) {
				file.mkdirs();
			}
			if(path.contains("\\")) { 
				path = plugin.getDataFolder().getCanonicalPath() + "\\" + "config.cfg";
			}	else {
				path = plugin.getDataFolder().getCanonicalPath() + "/" + "config.cfg";
			}
			file = new File(path);
			if(file.exists()) {
				BufferedReader in = new BufferedReader(new FileReader(file));
				String tmp;
				while ((tmp = in.readLine()) != null) {
					if(!tmp.startsWith("//") && tmp.contains("=")) {
						tmp.replace(" ", "");
						String tmp2[] = tmp.split("=");
						cf.put(tmp2[0], tmp2[1]);
					}
				}
			} else {
				file.createNewFile();
				plugin.logger.info("[" + plugin.getDescription().getName() + "] No config.cfg detected. Creating new one.");
				DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
				InputStream is = plugin.getClass().getResourceAsStream("/config.cfg");
				int c;
				while((c = is.read()) != -1) {
					out.writeByte(c);
				}
				is.close();
				out.close();
				load();
			}
		}	catch(Exception exc) {
			plugin.logger.info("ERROR: GuildEra.configParser.java:load() " + exc.toString());
		}
	}
	
	public String getString(String key) {
		try {
			return cf.get(key);
		} catch(Exception ex) {
			plugin.logger.info("[" + plugin.getDescription().getName() + "] Missing " + key + "(String) in the config file. Please update your config file!");
			plugin.logger.info("[" + plugin.getDescription().getName() + "] If " + key + "(String) is not missing in the config file, grab a developer.");
		}
		return null;
	}
	
	public double getDouble(String key) {
		try {
			return Double.parseDouble(cf.get(key));
		} catch(Exception ex) {
			plugin.logger.info("[" + plugin.getDescription().getName() + "] Missing " + key + "(double) in the config file. Please update your config file!");
			plugin.logger.info("[" + plugin.getDescription().getName() + "] If " + key + "(double) is not missing in the config file, grab a developer.");
		}
		return 0;
	}
	
	public int getInt(String key) {
		try {
			return Integer.parseInt(cf.get(key));
		} catch(Exception ex) {
			plugin.logger.info("[" + plugin.getDescription().getName() + "] Missing " + key + "(int) in the config file. Please update your config file!");
			plugin.logger.info("[" + plugin.getDescription().getName() + "] If " + key + "(int) is not missing in the config file, grab a developer.");
		}
		return 0;
	}
	
	public boolean getBool(String key) {
		try {
			if(cf.get(key).equalsIgnoreCase("true")) {
				return true;
			} else {
				return false;
			}
		} catch(Exception ex) {
			plugin.logger.info("[" + plugin.getDescription().getName() + "] Missing " + key + "(boolean) in the config file. Please update your config file!");
			plugin.logger.info("[" + plugin.getDescription().getName() + "] If " + key + "(boolean) is not missing in the config file, grab a developer.");
		}
		return false;
	}
}
